<template>
    <div>
        <SkuForm
            ref="skuForm"
            :source-attribute="sourceAttribute"
            :structure="structure"
            :attribute.sync="attribute"
            :sku.sync="sku"
        />
        <el-button type="primary" style="margin-top: 10px;" @click="submit">提交</el-button>
        <el-row type="flex" :gutter="20">
            <el-col>
                <el-divider content-position="left">attribute 数据</el-divider>
                <pre><code>{{ attribute }}</code></pre>
            </el-col>
            <el-col>
                <el-divider content-position="left">sku 数据</el-divider>
                <pre><code>{{ sku }}</code></pre>
            </el-col>
        </el-row>
    </div>
</template>

<script>
export default {
    data() {
        return {
            sourceAttribute: [
                {
                    name: '颜色',
                    item: ['黑', '金', '白']
                },
                {
                    name: '内存',
                    item: ['16G', '32G']
                }
            ],
            structure: [
                {
                    name: 'goods_code',
                    type: 'input',
                    label: '商品唯一编码',
                    tip: '模拟异步操作，如果输入123则会提示编码已存在',
                    required: true,
                    // data: 完整 sku 数据，index: 当前 sku 在 data 中的下标，callback: 验证结果回调函数
                    validate: (data, index, callback) => {
                        setTimeout(() => {
                            if (data[index].goods_code == '123') {
                                callback(new Error('商品唯一编码已存在'))
                            } else {
                                callback()
                            }
                        }, 1000)
                    }
                },
                {
                    name: 'price',
                    type: 'input',
                    label: '价格'
                },
                {
                    name: 'stock',
                    type: 'input',
                    label: '库存'
                }
            ],
            attribute: [],
            sku: []
        }
    },
    methods: {
        submit() {
            this.$refs.skuForm.validate(valid => {
                if (valid) {
                    this.$message.success('验证通过')
                } else {
                    this.$message.warning('验证失败')
                }
            })
        }
    }
}
</script>